library(Mediana)

# A Ph3 trial enroll pts with mulitple sclerosis.
# The endpoint is the number of new lesions seen of the brain.

# The distribution of count-type endpoint is a 'negative binomal distribution'.

# [Data Model] ----
## Outcome parameters
outcome.placebo = parameters(dispersion = 0.5, mean = 13)
outcome.treatment = parameters(dispersion = 0.5, mean = 7.8)

## Data model
counttrial.dm = DataModel() +
  OutcomeDist(outcome.dist = "NegBinomDist") +
  SampleSize(seq(100, 150, 10)) +
  Sample(id = "Placebo", outcome.par = parameters(outcome.placebo)) +
  Sample(id = "Treatment", outcome.par = parameters(outcome.treatment))
  
# [Analysis Model] ----
## The efficacy will be assessed by a negative binomal generalized linear model (NBGLM)
counttrial.am = AnalysisModel() +
  Test(id = "Treatment vs Placebo",
       samples = samples("Treatment", "Placebo"),
       method = "GLMNegBinomTest")

# [Evaluation Model] ----
counttrial.em = EvaluationModel() +
  Criterion(id = "Marginal power",
            method = "MarginalPower",
            tests = tests("Treatment vs Placebo"),
            labels = c("Treatment vs Placebo"),
            par = parameters(alpha = 0.025))

# [Clinical Scenario Evaluation] ----
## Simulation parameters
counttrial.sp = SimParameters(n.sims = 1000, proc.load = "full", seed = 4293)

## Perform clinical scenario evaluation
counttrial.rst = CSE(counttrial.dm, counttrial.am, counttrial.em, counttrial.sp)

# [Reporting] ----
## Presentation model
counttrial.pm = PresentationModel() +
  Project(username = "[Mediana's User]",
          title = "Count-type endpoint trial",
          description = "Study in pts with multiple sclerosis") +
  Section(by = c("outcome.parameter")) +
  Table(by = c("sample.size")) +
  CustomLabel(param = "sample.size",
              label = paste0("N=", seq(100, 150, 10)))

## Generate report
GenerateReport(presentation.model = counttrial.pm,
               cse.results = counttrial.rst,
               report.filename = "CountTypeEndpoint.docx")

## Get the data generated in the CSE
counttrial.datastack = DataStack(data.model = counttrial.dm,
                                 sim.parameters = counttrial.sp)
